package fdts.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;

import prefuse.data.expression.parser.ParseException;

import timeBench.data.TemporalDataset;

class myParser{
	static final String SPACE = " " ;
	static final String TAB = "\t";
	static final String CRLF = "\r\n";
	
	static final int STATE_NONE = 0;
	static final int STATE_VCALENDAR = 1;
	static final int STATE_VEVENT = 2;
	static final int STATE_VTODO = 3;
	static final int STATE_VJOURNAL = 4;
	static final int STATE_VTIMEZONE = 5;
	static final int STATE_VFREEBUSY = 6;
	static final int STATE_VALARM = 7;
	static final int STATE_STANDARD = 8;
	static final int STATE_DAYLIGHT = 9;
	static final int STATE_DONE = 10;
	
	public myParser(){
		
	}
	
	public TemporalDataset parse( Reader reader ) throws IOException{
		TemporalDataset dataset = new TemporalDataset();
		String line = "";
		String nextLine = "";
		BufferedReader r = new BufferedReader ( reader );
		Integer prevState = STATE_NONE;
		ArrayList<Integer> states = new ArrayList<Integer>();
		states.add(STATE_NONE);
		int linenumber = 0;
		StringBuffer data = new StringBuffer();
		boolean done = false;
		
		nextLine = r.readLine();
		if(nextLine == null){
			throw new ParseException("BEGIN:VCALENDAR is missing in file"); // empty file
		}
		
		while( !done ){
			String lineUpperCase = "";
			
			if(nextLine != null){
				line = nextLine;
				linenumber++;
				nextLine = r.readLine();
				
				data.append(line);
				lineUpperCase = line.toUpperCase();
			} else {
				done = true;
			}
			
			// unfold lines
			if(nextLine != null && nextLine.length() > 0 && 
					( SPACE.equals( String.valueOf(nextLine.charAt(0)) )  || TAB.equals( String.valueOf(nextLine.charAt(0)) ) ) ){
				nextLine = line + CRLF + nextLine;
				continue;
			}
			
			switch(states.get(states.size()-1)){
				case STATE_NONE: {
					if( lineUpperCase.startsWith("BEGIN:VCALENDAR")){
						states.add(STATE_VCALENDAR);
					} else if( lineUpperCase.length() == 0 )
						; // do nothing when blank line
					else {
						throw new ParseException("BEGIN:VCALENDAR is missing in file");
					}
					break;
				}
				case STATE_VCALENDAR: {
					if( lineUpperCase.startsWith("X-WR-TIMEZONE:")){
						// TODO
						System.out.println(  "VCALENDAR X-WR-TIMEZONE do something" );
					} else if( lineUpperCase.startsWith("CALSCALE:")){
						// TODO
						System.out.println(  "VCALENDAR CALSCALE do something" );
					} else if( lineUpperCase.startsWith("BEGIN:VTIMEZONE")){
						states.add(STATE_VTIMEZONE);
					} else if(  lineUpperCase.startsWith("BEGIN:VEVENT") ){
						states.add(STATE_VEVENT);
					} else if( lineUpperCase.startsWith("BEGIN:VTODO") ){
						states.add(STATE_VTODO);
					} else if( lineUpperCase.startsWith("BEGIN:VJOURNAL") ){
						states.add(STATE_VJOURNAL);
					} else if( lineUpperCase.startsWith("BEGIN:VFREEBUSY") ){
						states.add(STATE_VFREEBUSY);
					} else if( lineUpperCase.startsWith("END:VCALENDAR") ){
						System.out.println( "VCALENDAR is not yet implemented." );
						states.remove(states.size()-1);
						states.add(STATE_DONE);
					} else {
						// TODO
						System.out.println( "Unrecognized content in VCALENDAR: " + line );
					}
					break;
				}
				case STATE_VEVENT: {
					if( lineUpperCase.startsWith("END:VEVENT") ){
						System.out.println( "VEVENT is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						//TODO
					}
					break;
				}
				case STATE_VTODO: {
					// TODO
					if( lineUpperCase.startsWith("BEGIN:VALARM") ){
						states.add(STATE_VALARM);
					} else if ( lineUpperCase.startsWith("END:VTODO") ){
						System.out.println( "VTODO not yet implemented." ); 
						states.remove(states.size()-1);
					} else {
						// unecognized content
					}
					break;
				}
				case STATE_VJOURNAL: {
					if( lineUpperCase.startsWith("END:VJOURNAL") ){
						System.out.println( "VJOURNAL is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						// TODO
						System.out.println( " Unrecognized content in VJOURNAL: " + line );	
					}
					break;
				} 
				case STATE_VFREEBUSY: {
					// TODO
					if( lineUpperCase.startsWith("END:VFREEBUSY") ){
						System.out.println( "VFREEBUSY is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						// TODO
					}
					break;
				}
				case STATE_VTIMEZONE: {
					if( lineUpperCase.startsWith("BEGIN:DAYLIGHT") ){
						states.add(STATE_DAYLIGHT);
					} else if( lineUpperCase.startsWith("BEGIN:STANDARD") ){
						states.add(STATE_STANDARD);
					} else if( lineUpperCase.startsWith("END:VTIMEZONE") ){
						System.out.println( "VTIMEZONE is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						// TODO
						System.out.println( "Unrecognized content in VTIMEZONE: " + line );
					}
					break;
				}
				case STATE_VALARM: {
					if( lineUpperCase.startsWith("END:VALARM") ){
						System.out.println( "VALARM is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						//TODO
					}
					break;
				}
				case STATE_STANDARD: {
					if( lineUpperCase.startsWith("END:STANDARD") ){
						System.out.println( "STANDARD is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						//TODO
					}
					break;
				}
				case STATE_DAYLIGHT: {
					if( lineUpperCase.startsWith("END:DAYLIGHT") ){
						System.out.println( "DAYLIGHT is not yet implemented." );
						states.remove(states.size()-1);
					} else {
						//TODO
					}
					break;
				}
				case STATE_DONE: {
					// TODO
					System.out.println( "done" );
					if(!done) 
						throw new ParseException("There should be nothing after END:VCALENDAR!");
					break;
				}
				default: {
					throw new ParseException("State is invalid!");
				}
			}
		}
		
		
		return dataset;
	}
}